﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TrainingDemo.Util.NativeUtil;
using System.Runtime.InteropServices;
using System.IO;

namespace TrainingDemo.Model
{
    public class TechnologyAccessibility : TechnologyBridge
    {
        #region Private Property

        static TechnologyAccessibility m_instance   = null;
        IntPtr handleModule                         = IntPtr.Zero;

        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
        private delegate int GetChildren(int topWindowHandle, StringBuilder children, int maxLength);

        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
        private delegate int GetSupportedProperty(string controlName, StringBuilder properties, int maxlength);

        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
        private delegate int GetProperty(string controlName, string propertyName, StringBuilder propertyValue, int maxlength);

        private GetChildren funcGetChildren                     = null;
        private GetSupportedProperty funcGetSupportedProperty   = null;
        private GetProperty funcGetProperty                     = null;

        #endregion


        #region Construction/Destruction/Init


        public TechnologyAccessibility() : base(Const.TECHNOLOGY_ACCESIBILITY) 
        {
            //String currentDirectory = Directory.GetCurrentDirectory() + "\\" + Const.ACC_MODULE_NAME;
            //handleModule = NativeApiHandler.LoadLibrary(currentDirectory);
            //IntPtr addressOfFunc = IntPtr.Zero;
            //if (handleModule == IntPtr.Zero)
            //{
            //    throw new Exception(String.Format("Can not load library: \"{0}\".", currentDirectory));
            //}
            //else
            //{
            //    addressOfFunc = NativeApiHandler.GetProcAddress(handleModule, Const.FUNC_NAME_GET_CHILDREN);
            //    if (addressOfFunc == IntPtr.Zero)
            //    {
            //        throw new Exception(String.Format("Can not load method: \"{0}\".", Const.FUNC_NAME_GET_CHILDREN));
            //    }
            //    else
            //    {
            //        funcGetChildren = (GetChildren)Marshal.GetDelegateForFunctionPointer(addressOfFunc, typeof(GetChildren));
            //        addressOfFunc = IntPtr.Zero;
            //    }

            //    addressOfFunc = NativeApiHandler.GetProcAddress(handleModule, Const.FUNC_NAME_GET_SUPPORTED_PROPERTIES);
            //    if (addressOfFunc == IntPtr.Zero)
            //    {
            //        throw new Exception(String.Format("Can not load method: \"{0}\".", Const.FUNC_NAME_GET_SUPPORTED_PROPERTIES));
            //    }
            //    else
            //    {
            //        funcGetSupportedProperty = (GetSupportedProperty)Marshal.GetDelegateForFunctionPointer(addressOfFunc, typeof(GetSupportedProperty));
            //        addressOfFunc = IntPtr.Zero;
            //    }

            //    addressOfFunc = NativeApiHandler.GetProcAddress(handleModule, Const.FUNC_NAME_GET_PROPERTY);
            //    if (addressOfFunc == IntPtr.Zero)
            //    {
            //        throw new Exception(String.Format("Can not load method: \"{0}\".", Const.FUNC_NAME_GET_PROPERTY));
            //    }
            //    else
            //    {
            //        funcGetProperty = (GetProperty)Marshal.GetDelegateForFunctionPointer(addressOfFunc, typeof(GetProperty));
            //        addressOfFunc = IntPtr.Zero;
            //    }
            //}
        }

        public static TechnologyAccessibility getInstance()
        {
            if (m_instance == null)
            {
                m_instance = new TechnologyAccessibility();
            }
            return m_instance;
        }

        #endregion


        #region Private methods


        #endregion


        #region Public methods

        public override int getChildren(IntPtr nHandle, ref string szChildrenName, int maxLength)
        {
            int nResult = Const.AUTO_FALSE;
            StringBuilder children = new StringBuilder(maxLength);
            if (funcGetChildren != null)
            {
                nResult = funcGetChildren(nHandle.ToInt32(), children, maxLength);
                if (nResult == Const.AUTO_OK)
                {
                    szChildrenName = children.ToString();
                }
            }
            return nResult;
        }

        public override int getSupportedProperty(string szControlName, ref string szProperties, int maxLength)
        {
            int nResult = Const.AUTO_FALSE;
            StringBuilder properties = new StringBuilder(maxLength);
            if (funcGetChildren != null)
            {
                nResult = funcGetSupportedProperty(szControlName, properties, maxLength);
                if (nResult == Const.AUTO_OK)
                {
                    szProperties = properties.ToString();
                }
            }
            return nResult;
        }

        public override int getProperty(string szControlName, string szPropertyName, ref string szPropertyValue, int maxLength)
        {
            int nResult = Const.AUTO_FALSE;
            StringBuilder propertyValue = new StringBuilder(maxLength);
            if (funcGetChildren != null)
            {
                nResult = funcGetProperty(szControlName, szPropertyName, propertyValue, maxLength);
                if (nResult == Const.AUTO_OK)
                {
                    szPropertyValue = propertyValue.ToString();
                }
            }
            return nResult;
        }

        #endregion
    }
}
